<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends AppController
{
	
	function __construct()
	{
		parent::__construct();
	}
	public function index()
	{

	}
	public function view($id = NULL)
	{
		
		if(empty($id)){
			redirect('/');
		}

		$news = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);

		if(empty($news))
			redirect('/');

		$this->data['news'] = $news;
		$this->render('news/view');
	}
	
	public function admin_index($sortBy= 'id',$sortOrder = 'desc'){
		$pag = array(
			'uri_segment' =>6,
			'sort' =>true,
			'per_page' =>10,
			'sortList'=>array('stt'=>'id','title'=>'title','date'=>'date','content'=>'content','sticky'=>'sticky','active'=>'active'),
			'base_url' => '/admin/news/index',
			'sortBy' => $sortBy,
			'sortOrder'=>$sortOrder
			);
		$pag = $this->paginate($pag);
		$pag['total_rows'] = $this->getEM()->getRepository('Entity\ShNews')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['news'] = $this->getEM()->getRepository('Entity\ShNews')->backendPaginate($pag);
		$this->data['breadcrumb']['Tin tức'] =  null;
		$this->data['breadcrumb']['ext'] = array(
			'button' => array('url'=> 'news/add','name'=> 'Thêm Tin Tức'),
			);
		$this->render('news/admin_index',array('back.js'));
	}
	public function mod_index($sortBy= 'id',$sortOrder = 'desc'){
		$pag = array(
			'uri_segment' =>6,
			'sort' =>true,
			'per_page' =>10,
			'sortList'=>array('stt'=>'id','title'=>'title','date'=>'date','content'=>'content','sticky'=>'sticky','active'=>'active'),
			'base_url' => '/mod/news/index',
			'sortBy' => $sortBy,
			'sortOrder'=>$sortOrder
			);
		$pag = $this->paginate($pag);
		$pag['total_rows'] = $this->getEM()->getRepository('Entity\ShNews')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['news'] = $this->getEM()->getRepository('Entity\ShNews')->backendPaginate($pag);
		$this->data['breadcrumb']['Tin tức'] =  null;
		$this->data['breadcrumb']['ext'] = array(
			'button' => array('url'=> 'news/add','name'=> 'Thêm Tin Tức'),
			);
		$this->render('news/mod_index',array('back.js'));
	}

	public function admin_add(){
		$this->news_add();
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/admin/news'),
			'Thêm tin tức' => null
		);
		$this->render('news/admin_add',
			array('jquery.sceditor.bbcode.min','language/vi','sceditor','thumbnail-module'),
			array('default.min','tabs')
		);
	}
	public function mod_add(){
		$this->validation();

		if($this->input->post() && $this->form_validation->run('news')):
			if($this->getEM()->getRepository('Entity\ShNews')->addNews($this->input)):
				$this->log(9,array($this->data['user']));
				$this->session->set_flashdata('success','Thêm tin tức thành công');
				redirect('/mod/news');
			else:
				$this->data['flash'] = array('error' => 'Lỗi sql, thử lại');
			endif;
		endif;
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/mod/news'),
			'Thêm tin tức' => null
		);
		$this->render('news/mod_add',
			array('jquery.sceditor.bbcode.min','language/vi','sceditor','thumbnail-module'),
			array('default.min','tabs')
		);
	}
	private function news_add(){

		$this->validation();

		if($this->input->post() && $this->form_validation->run('news')):
			if($this->getEM()->getRepository('Entity\ShNews')->addNews($this->input)):
				$this->log(9,array($this->data['user']));
				$this->session->set_flashdata('success','Thêm tin tức thành công');
				redirect('/admin/news');
			else:
				$this->data['flash'] = array('error' => 'Lỗi sql, thử lại');
			endif;
		endif;
		return true;
	}
	
	public function mod_edit($id = null){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã tin tức');
			redirect('/mod/news');
		endif;
		
		$this->news_delete($id,'mod');
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/mod/news'),
			'Sửa tin tức {'.$id.'}' => null
		);
		$this->data['breadcrumb']['ext']  = array(
			'button' => array('url' => 'news/add', 'name' => 'Thêm tin tức'),
		);
		$this->render('news/mod_edit',
			array('jquery.sceditor.bbcode.min','language/vi','sceditor','thumbnail-module'),
			array('default.min','tabs')
		);
	}
	public function admin_edit($id = null){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã tin tức');
			redirect('/admin/news');
		endif;
		
		$this->news_edit($id);
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/admin/news'),
			'Sửa tin tức {'.$id.'}' => null
		);
		$this->data['breadcrumb']['ext']  = array(
			'button' => array('url' => 'news/add', 'name' => 'Thêm tin tức'),
		);
		$this->render('news/admin_edit');
	}
	private function news_edit($id, $url = 'admin'){
		$repo = $this->getEM()->getRepository('Entity\ShNews');
		$this->data['news'] = $repo->findOneById($id);
		if(empty($this->data['news'])):
			$this->session->set_flashdata('error','Không có tin tức này');
			redirect('/'.$url.'/news');
		endif;
		
		$this->validation();
		if($this->input->post() && $this->form_validation->run('news')):

			$news = $this->getEM()->getRepository('Entity\ShNews')->updateNews($this->input, $this->data['news']);
			if($news):
				 $this->log(11,array($this->data['user']));
				$this->session->set_flashdata('success','Sửa tin tức thành công');
				redirect('/'.$url.'/news');
			else:
				$this->data['flash'] = array('error' => 'Lỗi sql, thử lại');
			endif;
		endif;
	}
	
	public function mod_delete($id = null){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã tin tức');
			redirect('/mod/news');
		endif;

		
		$this->news_delete($id,'mod');
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/mod/news'),
			'Xóa tin tức {'.$id.'}' => null
		);
		$this->data['breadcrumb']['ext']  = array(
			'button' => array('url' => 'news/add', 'name' => 'Thêm tin tức'),
		);
		$this->render('news/mod_delete');
	}

	public function admin_delete($id = null){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã tin tức');
			redirect('/admin/news');
		endif;

		
		$this->news_delete($id);
		$this->data['breadcrumb'] = array(
			'Tin tức' => base_url('/admin/news'),
			'Xóa tin tức {'.$id.'}' => null
		);
		$this->data['breadcrumb']['ext']  = array(
			'button' => array('url' => 'news/add', 'name' => 'Thêm tin tức'),
		);
		$this->render('news/admin_delete');
	}
	private function news_delete($id, $url = 'admin'){
		$this->data['news'] = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);
		$this->data['user'] = $this->getEM()->getRepository('Entity\ShUser')->findOneById($this->session->userdata['uid']);
		if(empty($this->data['news'])):
			$this->session->set_flashdata('error','Không có tin tức này');
			redirect('/'.$url.'/news');
		endif;

		$this->validation();
		if($this->input->post() /*&& $this->form_validation->run('captcha')*/):

			if($this->getEM()->getRepository('Entity\ShNews')->deleteNews($this->data['news'])):
			    $this->log(4,array($this->data['user']));
				$this->session->set_flashdata('success', 'Xóa tin tức thành công');
				redirect('/'.$url.'/news');
			else:
				$this->data['flash'] = array('error' => 'Lỗi sql, thử lại');
			endif;
		endif;
	}
	public function admin_shutdown($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Tin');
		redirect('admin/news');
		endif;

		$news = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);
		if(empty($news)):
			$this->session->set_flashdata('error','Không có tin này');
		redirect('admin/news');
		endif;
		$sticky = $news->getSticky();
		$news->setSticky(!$sticky);
		$this->getEM()->persist($news);
		$this->getEM()->flush();
		 $this->log(9,array($this->data['user']));
		redirect('admin/news');
	}
	public function admin_active($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Tin');
		redirect('admin/news');
		endif;

		$act = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);
		if(empty($act)):
			$this->session->set_flashdata('error','Không có tin này');
		redirect('admin/news');
		endif;
		$active = $act->getActive();
		$act->setActive(!$active);
		$this->getEM()->persist($act);
		$this->getEM()->flush();
		 $this->log(9,array($this->data['user']));
		redirect('admin/news');
	}
	public function mod_shutdown($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Tin');
		redirect('mod/news');
		endif;

		$news = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);
		if(empty($news)):
			$this->session->set_flashdata('error','Không có tin này');
		redirect('mod/news');
		endif;
		$sticky = $news->getSticky();
		$news->setSticky(!$sticky);
		$this->getEM()->persist($news);
		$this->getEM()->flush();
		 $this->log(9,array($this->data['user']));
		redirect('mod/news');
	}
	public function mod_active($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Tin');
		redirect('mod/news');
		endif;

		$act = $this->getEM()->getRepository('Entity\ShNews')->findOneById($id);
		if(empty($act)):
			$this->session->set_flashdata('error','Không có tin này');
		redirect('mod/news');
		endif;
		$active = $act->getActive();
		$act->setActive(!$active);
		$this->getEM()->persist($act);
		$this->getEM()->flush();
		 $this->log(9,array($this->data['user']));
		redirect('mod/news');
	}
}
?>